package com.zkx.view;


import com.zkx.action.GoddessAction;
import com.zkx.model.Goddess;

import java.sql.Date;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;

public class View {
    private static final String CONTEXT="欢迎来到女神禁区：\n"+
            "下面是女神禁区的功能列表：\n"+
            "[MAIN/M]:主菜单 \n"+
            "[QUERY/Q]:查看女神的全部信息 \n"+
            "[GET/G]:查看某位女神的详细信息 \n"+
            "[INSERT/I]:添加女神信息 \n"+
            "[UPDATE/U]:更新女神信息 \n"+
            "[DELETE/D]:删除女神 \n"+
            "[SELECT/S]:查询女神信息（根据姓名，手机号来查询） \n"+
            "[EXIT/E]:退出女神禁区 \n"+
            "[BREAK/B]:退出当前功能，返回主菜单";
    private static final String OPERATION_MAIN="MAIN";
    private static final String OPERATION_QUERY="QUERY";
    private static final String OPERATION_GET="GET";
    private static final String OPERATION_INSERT="INSERT";
    private static final String OPERATION_UPDATE="UPDATE";
    private static final String OPERATION_DELETE="DELETE";
    private static final String OPERATION_SELECT="SELECT";
    private static final String OPERATION_EXIT="EXIT";
    private static final String OPERATION_BREAK="BREAK";
    public static void main(String[] args) throws ParseException, SQLException {
        System.out.println(CONTEXT);
        Scanner scan = new Scanner(System.in);
        //怎么保持程序一直运行
        //scan.hasNext() 判断是否还有输入的数据
        //next()
        /*1、一定要读取到有效字符后才可以结束输入。
        2、对输入有效字符之前遇到的空白，next() 方法会自动将其去掉。
        3、只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
        next() 不能得到带有空格的字符串。*/
        Goddess goddess = new Goddess();
        GoddessAction action = new GoddessAction();
        String previous = null;
        int step = 1;
        while(scan.hasNext()){
            String in = scan.next().toString();
            if(OPERATION_EXIT.equals(in.toUpperCase())||OPERATION_EXIT.substring(0,1).equals(in.toUpperCase())){
                System.out.println("成功退出程序");
                break;
            }else if(OPERATION_INSERT.equals(in.toUpperCase())
                    ||OPERATION_INSERT.substring(0,1).equals(in.toUpperCase())
                    ||OPERATION_INSERT.equals(previous)){
                previous = OPERATION_INSERT;
                if(1==step){
                    System.out.println("姓名： ");
                }else if(2 == step){
                    goddess.setUser_name(in);
                    System.out.println("性别： ");
                }else if(3 == step){
                    goddess.setSex(Integer.valueOf(in));
                    System.out.println("年龄： ");
                }else if(4 == step){
                    goddess.setAge(Integer.valueOf(in));
                    System.out.println("生日，格式如yyyy-mm-dd： ");
                }else if(5 == step){
                    SimpleDateFormat sf = new SimpleDateFormat("yyyy-mm-dd");
                    java.util.Date birthday =sf.parse(in);
                    goddess.setBirthday(birthday);
                    System.out.println("邮箱： ");
                }else if(6 == step){
                    goddess.setEmail(in);
                    System.out.println("手机号： ");
                }else if(7 == step){
                    goddess.setMobile(in);
                    System.out.println("isdel: ");
                }
                else if(8 == step){
                    goddess.setIsdel(Integer.valueOf(in));
                    action.insert(goddess);
                    System.out.println("新增女神成功");
                }
                if(OPERATION_INSERT.equals(previous)){
                    step++;
                }
            }
            else if(OPERATION_QUERY.equals(in.toUpperCase())||OPERATION_QUERY.substring(0,1).equals(in.toUpperCase())){
                List<Goddess> list =  action.select();
                for(Goddess go:list){
                    System.out.println(go.getId()+",姓名： "+go.getUser_name());
                }
            }
        }
    }
}
